//
//  PieChartViewController.m
//  Stock
//
//  Created by blacksun on 11-10-9.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "PieChartViewController.h"

@implementation PieChartViewController
@synthesize titleDict;


-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    
    [loadingView removeFromSuperview];
    self.loadingView = nil;
    
    self.upDatas = [aResult objectForKey:@"opinions"];

    [self buildChartView];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleDict = [NSDictionary dictionaryWithObjectsAndKeys:@"新闻报道",@"10",@"机构研报",@"20",@"专家荐股",@"30",@"名博",@"40",@"股吧论坛",@"50", nil];
}

-(void)buildChartView{
    CPTXYGraph *barChart = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    [barChart applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
    
    self.graph = barChart;
    ((CPTGraphHostingView *)self.view).hostedGraph = barChart;
    
    barChart.plotAreaFrame.borderLineStyle = nil;
    barChart.plotAreaFrame.cornerRadius = 0.0f;
    
    barChart.paddingLeft = 0.0f;
    barChart.paddingRight = 0.0f;
    barChart.paddingTop = 0.0f;
    barChart.paddingBottom = 0.0f;
    
    barChart.plotAreaFrame.paddingLeft = -5.0f;
    barChart.plotAreaFrame.paddingRight = 0.0f;
    barChart.plotAreaFrame.paddingTop = 0.0f;
    barChart.plotAreaFrame.paddingBottom = -5.0f;
    
    // Overlay gradient for pie chart
    /*CPTGradient *overlayGradient = [[[CPTGradient alloc] init] autorelease];
    overlayGradient.gradientType = CPTGradientTypeRadial;
	overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.0];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.3] atPosition:0.9];
	overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.7] atPosition:1.0];
    */
    // Add pie chart
    CPTPieChart *piePlot = [[CPTPieChart alloc] init];
    piePlot.dataSource = self;
    piePlot.delegate = self;
    piePlot.pieRadius = MIN(0.7 * (barChart.frame.size.height - 2 * graph.paddingLeft) / 2.0,
                            0.7 * (barChart.frame.size.width - 2 * graph.paddingTop) / 2.0);
    piePlot.identifier = @"piePlot";
    //piePlot.paddingLeft = 5.0f;
    piePlot.startAngle = M_PI_4;
    piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
    //piePlot.overlayFill = [CPTFill fillWithGradient:overlayGradient];
    
    
    [barChart addPlot:piePlot];
    
    // Add legend
    /*CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
    theLegend.numberOfColumns = 1;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 2.0;
    
    barChart.legend = theLegend;
    
    barChart.legendAnchor = CPTRectAnchorTopRight;
    barChart.legendDisplacement = CGPointMake(-15.0, -15.0);
    */
    [piePlot release];
    
    
    [barChart release];
    
    [self.view bringSubviewToFront:loadingView];
}

-(void)loadChartData{
    NSString *uri = [NSString stringWithFormat:@"opinion/detail?id=%@&days=%@",stockId,chartType];
    [service queryChartDataWithUri:uri];
}


#pragma mark -
#pragma mark Plot Data Source Methods

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [upDatas count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    NSNumber *num;
    /*if (fieldEnum == CPTPieChartFieldSliceWidth) {
        NSLog(@"%d",index);
        num = (NSNumber *) [NSNumber numberWithInt:index];
    }else{
       */NSDictionary *dict = [upDatas objectAtIndex:index];
        NSString *score = [dict objectForKey:@"score"];
        num = (NSNumber *)[NSNumber numberWithInt:[score intValue]];
    //}
    NSLog(@"%d",[score intValue]);
    return /*(NSNumber *)[NSNumber numberWithInt:20];*/num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
    static CPTMutableTextStyle *whiteText = nil;
    
    if ( !whiteText ) {
        whiteText = [[CPTMutableTextStyle alloc] init];
        whiteText.color = [CPTColor whiteColor];
    }
    NSDictionary *dict = [upDatas objectAtIndex:index];
    int score = [[dict objectForKey:@"score"] intValue];
    NSString *type = [titleDict objectForKey:[dict objectForKey:@"type"]] ;
    
    CPTTextLayer *newLayer = [[[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@:%d",type,score]
                                                           style:whiteText] autorelease];
    return newLayer;
}
-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index{
    NSDictionary *dict = [upDatas objectAtIndex:index];
    NSString *type = [dict objectForKey:@"type"];
    return [titleDict objectForKey:type];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
